//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverExecuteHostage.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_player","_action_id","_freed_msg","_die_msg","_shared_free","_shared_alive","_free_code","_die_code","_evt_idx","_obj_var"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);
_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);

		_freed_msg = [_parameters, "FREED_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_shared_free = [_parameters, "SHARED_FREE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_free_code = [_parameters, "FREE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_free = _shared_free call FFUNC(getStringVar);
		_shared_alive = _shared_alive call FFUNC(getStringVar);
		
		_hostage_pos = getPosATL _object;
		_object setCaptive false;
		_object enableAI "AUTOTARGET";
		_object enableAI "MOVE";
		_object allowDamage false;
		
		if (typename _freed_msg == typename "") then {_freed_msg = [_freed_msg];};
		if (typename _die_msg == typename "") then {_die_msg = [_die_msg];};

		if (!isNil "_freed_msg") then {
			if (count _freed_msg > 0) then {[QGVAR(all_message),[_freed_msg,"all",objNull,"hq"],THIS_FILE] call FFUNC(callEvent);};
		};
		if (!isNil "_shared_free") then {
			if (_shared_free != "") then {
				[_instance_id,_shared_free,true] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_free,true],THIS_FILE] call FFUNC(callEvent);
			};
		};
		if (!isNil "_shared_alive") then {
			if (_shared_alive != "") then {
				[_instance_id,_shared_alive,alive _object] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_alive,alive _object],THIS_FILE] call FFUNC(callEvent);
			};
		};

		_evt_idx = [_object,_action,"HOSTAGE_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
		if (!isNil "_evt_idx" && _evt_idx >= 0) then {
			_object removeEventHandler ["killed", _evt_idx];
			TRACE_2("Removed Event",_evt_idx,"HOSTAGE_KILLED_EVENT");
			[_object,_action,"HOSTAGE_KILLED_EVENT",-1] call FFUNC(actionSetLocal);
		};

		if (_free_code != "") then {
			[QGVAR(all_execute),[_free_code,_object],THIS_FILE] call FFUNC(callEvent);
		};
		
		_obj_var = format["mission_person_freed_%1",_action_id];
		OSETVARP(_object,_obj_var,true);		
		TRACE_1("Object Var",ARR_3(_object,_obj_var,OGETVAR(_hostage,_obj_var,false)));
		
		[_object,_caller,_hostage_pos] spawn {
			private ["_object","_caller","_hostage_pos","_new_pos"];
			DEFAULT_PARAM(_object,0,objNull);
			DEFAULT_PARAM(_caller,1,objNull);
			DEFAULT_PARAM(_hostage_pos,2,objNull);
			if (alive _object) then {
				sleep 1;
				if (!isnull _caller) then {
					if ((_caller distance _object) < 10) then {
						_new_pos = (getPosATL _caller);
					} else {
						_new_pos = _hostage_pos;
					};
				} else {
					_new_pos = _hostage_pos;
				};
				_new_pos set[2, ((_new_pos select 2) + 0.1)];
				_object setPosATL _new_pos;
				sleep 5;
				_object allowDamage true;
			};
		};
		TRACE_2("Captive",_object, captive _object);
	};
};	
TRACE("End");
TMFPROFILERSTOP;